package com.czl.module_storehouse.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.module_base.fragment.BaseFragment;
import com.czl.module_base.http.HttpResponse;
import com.czl.module_base.mvp.presenter.InjectPresenter;
import com.czl.module_base.mvp.view.SimpleView;
import com.czl.module_base.utils.GlideUtils;
import com.czl.module_service.event.SortBeanEvent;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.adapter.StockInfoAdapter;
import com.czl.module_storehouse.databinding.ItemSelectGoodsBinding;
import com.czl.module_storehouse.databinding.LayoutRecyclerViewBinding;
import com.czl.module_storehouse.mvp.presenter.StockPresenter;
import com.jeremyliao.liveeventbus.LiveEventBus;

import java.util.ArrayList;

/**
 * @author by xubin
 * PS: Not easy to write code, please indicate.
 */
public class StockInfoFragment extends BaseFragment<LayoutRecyclerViewBinding> implements SimpleView {

    @InjectPresenter
    StockPresenter mStockPresenter;
    private String mSortCode;
    private Integer mSortId;

    @Override
    protected LayoutRecyclerViewBinding getBinding(LayoutInflater layoutInflater, ViewGroup container) {
        return LayoutRecyclerViewBinding.inflate(layoutInflater, container, false);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
    }

    @Override
    protected void reload() {
        super.reload();
        LiveEventBus.get(SortBeanEvent.class).observe(this, event -> loadData(event.getSort()));
        if (!TextUtils.isEmpty(mSortCode)) {
            loadData(null, mSortCode);
        }
        if (mSortId != null && mSortId > 0) {
            loadData(mSortId, null);
        }
    }

    private void loadData(SortBean sortBean) {
        if (sortBean == null) {
            return;
        }
        loadData(sortBean.getSortId(), null);
    }

    private void loadData(Integer sortId, String sortCode) {
        if (mStockPresenter != null) {
            mStockPresenter.getStorehouseSortStockInfo(sortId, sortCode);
        }
    }

    public void setSortCode(String sortCode) {
        this.mSortCode = sortCode;
    }

    public void setSortId(Integer sortId) {
        mSortId = sortId;
    }

    @Override
    public <T> void showHttpResponse(HttpResponse<T> httpResponse) {
        super.showHttpResponse(httpResponse);
        SortBean sortBean = (SortBean) httpResponse.getData();
        setupView(sortBean);
    }

    @SuppressLint("SetTextI18n")
    private void setupView(SortBean sortBean) {
        StockInfoAdapter adapter = new StockInfoAdapter(new ArrayList<>());
        binding.recyclerView.setAdapter(adapter);
        ItemSelectGoodsBinding binding = ItemSelectGoodsBinding.inflate(getLayoutInflater());
        adapter.addHeaderView(binding.getRoot());
        if (sortBean != null) {
            binding.tvGoodsName.setText(sortBean.getSortName());
            binding.tvGoodsCode.setText("编号：" + (sortBean.getSortCode() == null ? "" : sortBean.getSortCode()));
            binding.tvGoodsNorm.setText("规格：" + (sortBean.getSortModel() == null ? "" : sortBean.getSortModel()));
            GlideUtils.loadImage(getContext(), binding.ivGoods, sortBean.getSortImg(), R.mipmap.pic_default_loading, R.mipmap.pic_default_error);
            adapter.addAll(getContext(), sortBean);
        }
    }
}
